from django.core.exceptions import ValidationError
from django import forms
from web.forms.bootstrap import BootStrapForm
from web import models
from web.forms.widgets import ColorRadioSelect

class ProjectModelForm(BootStrapForm,forms.ModelForm):
    bootstrap_class_exclude = ['color']
    # desc=forms.CharField(widget=forms.Textarea(attrs={'xx':123}))
    class Meta:
        model=models.Project
        fields=['name','color','desc']
        widgets={
            'desc':forms.Textarea(),
            'color':ColorRadioSelect(attrs={'class':'color-radio'})
        }

    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.request = request

    def clean_name(self):
        name=self.cleaned_data['name']
        exists=models.Project.objects.filter(name=name,creator=self.request.tracer.user).exists()
        if exists:
            raise ValidationError('项目名已存在')

        count=models.Project.objects.filter(creator=self.request.tracer.user).count()
        if count >= self.request.tracer.price_policy.project_num:
            raise ValidationError('项目个数超过限制，请购买套餐！')

        return name
